package com.longy.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.longy.common.valid.AddGroup;
import com.longy.common.valid.UpdateGroup;
import com.longy.gulimall.product.mq.MessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.longy.gulimall.product.entity.BrandEntity;
import com.longy.gulimall.product.service.BrandService;
import com.longy.common.utils.PageUtils;
import com.longy.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author longyang
 * @email longyang3240@163.com
 * @date 2020-11-14 16:27:07
 */
@RestController
@RequestMapping("product/brand")
@Transactional
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private MessageSender messageSender;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/ma")
    public R ma(){
        Amtresp amtresp = new Amtresp();
        amtresp.setAmt("50");
        amtresp.setSite("hhahh");

        messageSender.sendMsg("asd");
        messageSender.sendMsg("test2.queue",amtresp);
        messageSender.sendMsg("dev_queue",amtresp);
        messageSender.sendMsg("topicQueue",amtresp);
        messageSender.sendTopicMsg(amtresp);


        return R.ok().put("page", "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand/*, BindingResult result*/){
//
//        if (result.hasErrors()){
//            Map<String, String> map = new HashMap<>();
//            //1.获取错误结果的信息
//            result.getFieldErrors().forEach((item)->{
//                //属性名
//                String field = item.getField();
//                //错误信息
//                String defaultMessage = item.getDefaultMessage();
//                map.put(field,defaultMessage);
//            });
//            return R.error(400,"提交的参数不合法").put("data",map);
//        }else {

            brandService.save(brand);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
